
package org.upt.dcda.presentation;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.upt.dcda.UserManagerBeanRemote;
import org.upt.dcda.db.UserEntity;
import org.upt.dcda.exceptions.BookstoreException;

/**
 * Servlet implementation class UserModifyInfoServlet
 */
@WebServlet("/UserModifyInfoServlet")
public class UserModifyInfoServlet extends HttpServlet
{

	@EJB
	private UserManagerBeanRemote	userManagerBean;

	private static final long		serialVersionUID	= 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UserModifyInfoServlet()
	{
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
			IOException
	{
		try
		{
			if (SessionAttributes.isLoggedIn(request))
			{
				String firstName = request.getParameter("firstName");
				String lastName = request.getParameter("lastName");
				String address = request.getParameter("address");
				UserEntity user = this.userManagerBean.modifyUserInfo(
						((UserEntity) request.getSession().getAttribute(SessionAttributes.USER)).getUserId(),
						firstName, lastName, address);
				request.getSession().setAttribute(SessionAttributes.USER, user);
			}
		}
		catch (BookstoreException e)
		{
			getServletContext().log("Caught BookstoreException: " + e, e);
		}
		finally
		{
			response.sendRedirect(request.getContextPath() + "/account.jsp");
		}
	}

}
